package com.kharthick.pwGame.map;

import java.awt.MediaTracker;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import com.kharthick.pwGame.players.Player;


public class MapParser extends DefaultHandler {
	Map map = null;
	List<Player> players;
	
	Vector<Tile> row;
	protected MediaTracker tracker = null;
	
	List<String> tileTypes;
	public final int TILE_SIZE;
	
	int numberOfPlayers;
	int currentRow;
	int currentColumn;

    public Map getMap(){
    	//if(null==map){
    	//	System.out.println("null map");
    	//}
    	
    	//Vector<Vector<Tile>> vectmap = map.getMap();
    	//System.out.println(vectmap.size() + "mapsize " );
    			//vectmap.get(0).size());
    	
    	return map;
    }
    
    public List<Player> getPlayers(){
    	return players;
    }
    
    //something weird is goign on..
    //Somehow the variables are disconnected
    //only way i get it to work is if i put it in here
    public MapParser (int numOfPlay, MediaTracker tracker) {
    	super();
    	
    	this.tracker = tracker;
    	TILE_SIZE = 50;
    	
    	numberOfPlayers = numOfPlay;
    	players = new ArrayList();
    	
    	tileTypes = new ArrayList<String>();
    	tileTypes.add("desert");
    	tileTypes.add("forest");
    	tileTypes.add("water");
    	tileTypes.add("mountain");
    	tileTypes.add("grass");
    }


    ////////////////////////////////////////////////////////////////////
    // Event handlers.
    ////////////////////////////////////////////////////////////////////


    public void startDocument (){
    	System.out.println("Start document");
    }


    public void endDocument (){
    	System.out.println("End document");
    	System.out.println("size of map " + map.getMap().size());
    }

    private void addTile(String type){
		// the tile's position on the map
		Vector<Integer> pos = new Vector<Integer>();
		pos.add(new Integer(currentColumn));
		pos.add(new Integer(currentRow));
    	
		row.add(map.createTile(type, pos));
		//System.out.println("addtile");
    }
    
    private void addUnit(String type, int possessor){
    	// unit's position on map
		Vector<Integer> pos = new Vector<Integer>();
		pos.add(new Integer(currentColumn));
		pos.add(new Integer(currentRow));
		
    	Player player;
    	
    	//Checks to see if player exists, other creates player
    	//If the player number is beyond the size of players list
    	//Fills in other players as thunks
    	if (players.size() > possessor){
    		player = players.get(possessor);
    		//if player doesnt
    		if(null == player){
    			player = new Player();
    			players.add(possessor, player);
    		}
    	}
    	else {
    		player = new Player();
    		for(int i = players.size(); i < possessor; i++){
    			players.add(null);
    		}
    		System.out.println("adding new players");
    		players.add(player);
    	}   		
    	
    	player.addUnit(Player.createUnit(type, pos, tracker));
    }

    public void startElement (String uri, String name,
			      String qName, Attributes atts){
    	if ("".equals (uri)){
		    System.out.println("Start element: " + qName);
		    if (qName.equals("map")){
		    	//players = new ArrayList();
		    	
		    	String mapName = atts.getValue("name");
		    	int maxPlayers =  new Integer(atts.getValue("maxPlayers"));
		    	int columns = new Integer(atts.getValue("columns"));
		    	int rows = new Integer(atts.getValue("rows"));
		    	
		    	//if(null == tracker)
	    		//	System.out.println("tracker is null");
		    	
		    	map = new Map(mapName, maxPlayers, columns, rows,
		    			TILE_SIZE, tracker);
		    	
		    	if(null==map){
		    		System.out.println("null map2");
		    	}
		    	System.out.println("do this");
		    }
		    else if (qName.equals("row")){
		    	row = new Vector<Tile>();
		    }
		    else if (tileTypes.contains(qName)){				
				addTile(qName);
				
		    	String unitType = atts.getValue("unitType");
		    	if(null != unitType){
		    		int unitPossessor = new Integer(atts.getValue("unitPossessor"));
		    		System.out.println("becha 0: " + numberOfPlayers);
		    		if(unitPossessor < numberOfPlayers){
		    			System.out.println("add unit");
		    			addUnit(unitType, unitPossessor);
		    		}
			    	System.out.println(unitType + unitPossessor);
		    	}
		    }
    	}
    	else
    		System.out.println("Start element: {" + uri + "}" + name);
    }


    public void endElement (String uri, String name, String qName){
    	if ("".equals (uri)){
		    System.out.println("End element: " + qName);
		    if (qName.equals("row")){
		    	//System.out.println("cr " + currentRow);
		    	currentRow++;
		    	currentColumn = 0;
		    	map.addRow(row);
		    }
		    else if (tileTypes.contains(qName)){
		    	//System.out.println("cc " + currentColumn);
		    	currentColumn++;
		    }
    	}
    	else
		    System.out.println("End element:   {" + uri + "}" + name);
    }

    public void characters (char ch[], int start, int length){
    	System.out.print("Characters:    \"");
    	for (int i = start; i < start + length; i++) {
    		switch (ch[i]) {
    		case '\\':
    			System.out.print("\\\\");
			break;
		    case '"':
			System.out.print("\\\"");
			break;
		    case '\n':
			System.out.print("\\n");
			break;
		    case '\r':
			System.out.print("\\r");
			break;
		    case '\t':
			System.out.print("\\t");
			break;
		    default:
			System.out.print(ch[i]);
			break;
		    }
		}
	System.out.print("\"\n");
    }

}
